package com.jic.veteran.controller;

import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.dto.ProjectDonationQueryParam;
import com.jic.veteran.model.ProjectDonationDetail;
import com.jic.veteran.service.ProjectDonationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "ProjectDonationController")
@Controller
@RequestMapping("/projectDonation")
public class ProjectDonationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectDonationController.class);

    @Autowired
    private ProjectDonationService projectDonationService;

    @ApiOperation("查询捐款列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProjectDonationDetail>> list(ProjectDonationQueryParam queryParam,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProjectDonationDetail> projectDonationDetailList = projectDonationService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(projectDonationDetailList));
    }

    @ApiOperation("根据ID获取捐款详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProjectDonationDetail> getItem(@PathVariable Long id) {
        ProjectDonationDetail projectDonationDetail = projectDonationService.getItem(id);
        return CommonResult.success(projectDonationDetail);
    }

    @ApiOperation("添加取捐款明细")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody ProjectDonationDetail projectDonationDetail) {
        int count = projectDonationService.create(projectDonationDetail);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
